<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<?php $this->load->view('admin/header.html')?>
<script type="text/javascript" src="<?php echo base_url()?>common/js/validateForm.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>common/js/MyJs.js"></script>
<script type="text/javascript">
    function Success(data) {
        $.Success(data);
    }
    UE.getEditor('contents');
    $(function () {
        $.validate_product('ajax_form');
    });

    function Del(product_id) {
        layer.confirm("确定删除", function () {
            $.post("/admin/product/del_product/" + product_id, null, function (data) {
                var jsonData = $.parseJSON(data);
                if (jsonData.Statu == 'ok') {
                    Success(jsonData)
                }
                else {
                    Success(jsonData)
                }
            })
        }, function () {
            layer.msg('取消删除', { icon: 0 });
        })
    }
    KindEditor.ready(function (K) {
        var editor = K.editor({
            uploadJson: '/common/kindeditor/php/upload_json.php',
            allowFileManager: true
        });
        function imgUpload() {
            editor.loadPlugin('multiimage', function () {
                editor.plugin.multiImageDialog({
                    clickFn: function (urlList) {
                        AddPic(urlList);

                        editor.hideDialog();
                    }
                });
            });
        }
        K('#imgurl').dblclick(function () {
            imgUpload();
            //one();
        });
        K('#btnImgUrl').click(function () {
            imgUpload();
            // one();
        });
    })


    function AddPic(imgs) {
        var urls = "";
        $.each(imgs, function (i, data) {
            if (urls == "") {
                urls = data.url;
            }
            else {
                urls = urls + "|" + data.url;
            }
        });
        AppendImg(urls)
    }

    function AppendImg(urls) {
        if (urls.length > 0) {
            var imgLength = 0;
            if ($("#imgurl").val().length > 0) {
                imgLength = $("#imgurl").val().split('|').length;
                $("#imgurl").val($("#imgurl").val() + "|" + urls)
            } else {
                $("#imgurl").val(urls);
            }
            var finalUrl = urls.split('|');
            var urlDiv = $("#img_li");
            for (i = 0; i < finalUrl.length; i++) {
                urlDiv.append('<li style="width:110px;height:130px;text-align:center;float:left;"><img style="height:100px;width:90px;" src="' + finalUrl[i] + '" /><div style="width:110px;height:20px;margin-top:10px;"><a href="javascript:void(0)" onclick="DelImg(' + (i + imgLength) + ')" style="border:1px solid #949090;color:#666666;padding:3px 6px;background-color:#F9F9F9;margin:0 auto;">删除</a></div></li>');
            }
        }
        else {
            $("#img_li").html("");
        }
    }

    function DelImg(ImgId) {
        var urls = $("#imgurl").val();
        if (ImgId >= 0) {
            var tempUrl = urls.split('|');
            var finalUrls = "";
            for (i = 0; i < tempUrl.length; i++) {
                if (i == ImgId) {
                    continue;
                }
                else {
                    if (finalUrls == "") {
                        finalUrls = tempUrl[i];
                    }
                    else {
                        finalUrls = finalUrls + "|" + tempUrl[i];
                    }
                }
            }
            $("#img_li").html("");
            $("#imgurl").val("");
            AppendImg(finalUrls);
        }
    }
    $(function () {
        var imgList = <?php echo !empty($_img_list)?$_img_list:'"";'?>;
        if (imgList.length > 0) {
            var finalUrl = $("#imgurl").val().split('|');
            var urlDiv = $("#img_li");
            for (i = 0; i < finalUrl.length; i++) {
                urlDiv.append('<li style="width:110px;height:130px;text-align:center;float:left;"><img style="height:100px;width:90px;" src="' + finalUrl[i] + '" /><div style="width:110px;height:20px;margin-top:10px;"><a href="javascript:void(0)" onclick="DelImg(' + i + ')" style="border:1px solid #949090;color:#666666;padding:3px 6px;background-color:#F9F9F9;margin:0 auto;">删除</a></div></li>');
            }
        }

    })
</script>
<div id="dcMain">
    <!-- 当前位置 -->
    <div id="urHere">
        DouPHP 管理中心
        <?php foreach($crumbs_items as $crumbs_item):?>
        <b>></b><a href="<?php echo $crumbs_item[0]?>"><?php echo $crumbs_item[1]?></a>
        <?php endforeach;?>
    </div>
    <div id="manager" class="mainBox" style="height:auto!important;height:550px;min-height:550px;">
        <h3><?php echo end($crumbs_items)[1]?></h3>
        <form id="ajax_form" action="<?php echo site_url('admin/product/edit_product')?>" method="post">
            <input id="product_id" name="product_id" type="hidden" value="<?php echo !empty($productModel)?$productModel->product_id:0?>" />
            <input id="menuid" name="menuid" type="hidden" value="<?php echo !empty($productModel)?$productModel->menuid:$menuid?>" />
            <input id="mark" name="mark" type="hidden" value="<?php echo !empty($productModel)?1:0?>" />
            <table width="100%" border="0" cellpadding="8" cellspacing="0" class="tableBasic">
                <tr>
                    <td width="100" align="right">标题</td>
                    <td>
                        <input type="text" id="title" name="title" size="40" class="inpMain" value="<?php echo !empty($productModel)?$productModel->title:""?>" />
                    </td>
                </tr>
                <tr>
                    <td width="100" align="right">关键字</td>
                    <td>
                        <textarea id="keyword" name="keyword" style="height:100px;width:665px;resize:none;"><?php echo !empty($productModel)?$productModel->keyword:""?></textarea>
                    </td>
                </tr>
                <tr>
                    <td width="100" align="right">描述</td>
                    <td>
                        <textarea id="description" name="description" style="height:100px;width:665px;resize:none;"><?php echo !empty($productModel)?$productModel->description:""?></textarea>
                    </td>
                </tr>
                <tr>
                    <td width="100" align="right">图片</td>
                    <td style="position:relative;">
                        <div style="width:100%;height:130px;">
                            <ul id="img_li"></ul><div></div>
                        </div>
                        <div style="clear:both;"></div>
                        <div style="display:block;width:100%;"><a style="border: 1px solid #949090; color: #666666; padding: 3px 6px; background-color: #F9F9F9;margin-right:10px;" href="javascript:void(0)" id="btnImgUrl">图片添加</a><input class="inpMain" type="text" id="imgurl" name="imgurl" readonly="readonly" style="cursor:pointer;" value="<?php echo !empty($productModel)?$productModel->imgurl:''?>" /></div>
                        <div>如需首页显示产品，为保证页面的美观，建议第一张产品图片的宽高比例为：3：5</div>
                    </td>
                </tr>
                <tr>
                    <td width="100" align="right">内容</td>
                    <td>
                        <textarea id="contents" name="contents" style="height:500px;width:965px;"><?php echo !empty($productModel)?$productModel->contents:""?></textarea>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" class="btn" value="<?php echo !empty($productModel)?"修改":"添加"?>" />
                        <?php if(!empty($productModel)){?>
                        <input type="button" class="btn" onclick="Del(<?php echo $productModel->product_id?>)" value="删除" />
                        <?php }?>
                        <input type="button" class="btn" value="返回" onclick="GoBack()" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>

